public class Leetcode2024 {

    public static void main(String[] args) {
        System.out.println(maxConsecutiveAnswers("TFFT", 1));
    }


    public static int maxConsecutiveAnswers(String answerKey, int k) {
        if (k == answerKey.length()) {
            return answerKey.length();
        }

        char[] answer = answerKey.toCharArray();

        return Math.max(getNotInCharNumberMaxLength(answer, 'T', k), getNotInCharNumberMaxLength(answer, 'F', k));
    }

    private static int getNotInCharNumberMaxLength(char[] answer, char k, int number) {
        int left = 0;
        int right = 0;
        int countK = 0;

        int max = 0;
        while (right < answer.length) {
            if (answer[right] == k) {
                countK++;
            }

            right++;

            while (countK > number) {
                if (answer[left] == k) {
                    countK--;
                }

                left++;
            }

            max = Math.max(right - left, max);
        }

        return max;
    }
}
